package com.easylinkin.linkappapi.safe.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.safe.dto.InspectionPlanDTO;
import com.easylinkin.linkappapi.safe.entity.InspectionPlan;
import com.easylinkin.linkappapi.safe.service.InspectionPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 自检计划管理
 */
@RestController
@RequestMapping("inspectionPlan")
@Api(tags = "自检计划管理")
public class InspectionPlanController {
  
  @Autowired
  private InspectionPlanService inspectionPlanService;

  /**
   * @Description: 增加自检计划
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping
  @ApiOperation("增加自检计划")
  public RestMessage insert(@RequestBody InspectionPlan inspectionPlan){
    //参数验证
    inspectionPlanService.insert(inspectionPlan);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 删除自检计划（包含批量删除）
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping
  @ApiOperation("删除自检计划（包含批量删除）")
  public RestMessage delBatch(@RequestBody InspectionPlan inspectionPlan){
    Assert.notEmpty(inspectionPlan.getIds(),"id不能为空");
    inspectionPlanService.delBatch(inspectionPlan.getIds());
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 编辑自检计划
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping
  @ApiOperation("编辑自检计划")
  public RestMessage updateById(@RequestBody InspectionPlan inspectionPlan){
    Assert.notNull(inspectionPlan.getId(),"id不能为空");
    inspectionPlanService.updateOne(inspectionPlan);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 发布/中止
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping("status")
  @ApiOperation("发布/中止")
  public RestMessage updateStatus(@RequestBody InspectionPlan inspectionPlan){
    Assert.notNull(inspectionPlan.getId(),"id不能为空");
    inspectionPlanService.updateStatus(inspectionPlan);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据id查询自检计划详情
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("根据id查询自检计划详情")
  public RestMessage findById(@PathVariable("id")Long id) {
    Assert.notNull(id,"id不能为空");
    InspectionPlan inspectionPlan = inspectionPlanService.getById(id);
    return RestBuilders.successBuilder().data(inspectionPlan).build();
  }

  /**
   * @Description: 根据条件，分页(不分页)查询
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，分页(不分页)查询")
  public RestMessage queryListByPage(@RequestBody RequestModel<InspectionPlan> requestModel){
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    IPage<InspectionPlanDTO> record =  inspectionPlanService.queryListByPage(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }
}

